package com.yuyou.fn.educational.discount

import com.alibaba.fastjson.JSON
import java.util.*;
import java.math.*;
import com.yuyou.fn.educational.discount.*;
import com.yuyou.fn.educational.entity.*;
import static com.yuyou.fn.common.util.BeanUtils.*;
import static com.yuyou.fn.common.util.Utils.*;

class ExecuteServiceImpl implements IExecuteService {


    static Map parsePeriodParams(period){
        def map = [:]

        period.split(",").each {
            def items  = it.split("#") as ArrayList

            if(items.size() == 1 ){
                map[items[0]] = ["1","2","3","4","5","6","7","8","9","10"] as ArrayList
            }else{
                map[items[0]] = items.subList(1,items.size())
            }
        }
        return map
    }


    @Override
    DiscountResultSet execute(List<DiscountRegCourseClass> discountRegCourseClassList, Map<String, Object> params, Helper helper) {

        def resultSet = new DiscountResultSet()
        def fromPeriodMaps = parsePeriodParams( params.fromPeriodNames )
        def toPeriodMaps = parsePeriodParams( params.toPeriodNames )
        discountRegCourseClassList.each {
            def discountRegCourseClass = it

            def discountRegClassInfoList = discountRegCourseClass.discountRegClassInfoList.grep{
                it.regDivSegment == 1 && it.fullReg && toPeriodMaps[it.periodName] != null && it.segmentNo + "" in toPeriodMaps[it.periodName]
            }

            //每哥科目在同一期只保留一个班
            discountRegClassInfoList = new ArrayList(toMap( discountRegClassInfoList){ it.subjectName + it.periodName}.values())

            def subjectId2DiscountRegClassInfo = toGroup( discountRegClassInfoList , "subjectId" )
            def classIds = []
            def subjectCnt = 0 //连报科数
            subjectId2DiscountRegClassInfo.each {
                def periodName2DiscountRegClassInfo = toGroup( it.value , "periodName" )

                if(periodName2DiscountRegClassInfo.size() == toPeriodMaps.size() ){
                    subjectCnt++
                    toPeriodMaps.each {
                        classIds.addAll( toList(periodName2DiscountRegClassInfo[it.key],'classId'))
                    }
                }
            }
            if(classIds.size() > 0 && subjectCnt == params.subjectCount ){

                def hasReg = false
                fromPeriodMaps.each {
                    if( !hasReg && helper.countStudentRegAmount(discountRegCourseClass.student.studentId,discountRegCourseClass.discountRegClassInfoList[0].areaTeamId,it.key )){
                        hasReg = true
                    }
                }
                if(hasReg ){
                    resultSet.add(discountRegCourseClass.student.studentId,classIds.size() * params.discount,classIds)
                }
            }


        }
        return resultSet




    }


    public static void main(String[] args) {

        def array = JSON.parseArray("[\n" +
                "\t{\n" +
                "\t\t\"discountRegClassInfoList\":[\n" +
                "\t\t\t{\n" +
                "\t\t\t\t\"areaTeamId\":564701030634225664,\n" +
                "\t\t\t\t\"areaTeamName\":\"广州区域\",\n" +
                "\t\t\t\t\"busTeamId\":1760234347461521408,\n" +
                "\t\t\t\t\"busTeamName\":\"秀勤业务组1\",\n" +
                "\t\t\t\t\"campusId\":1760158560570089472,\n" +
                "\t\t\t\t\"campusName\":\"秀勤校区\",\n" +
                "\t\t\t\t\"classDateTip\":\"每周六\",\n" +
                "\t\t\t\t\"classId\":1773920395341799424,\n" +
                "\t\t\t\t\"className\":\"2019秋高一数学AAA班\",\n" +
                "\t\t\t\t\"classNo\":\"259705\",\n" +
                "\t\t\t\t\"classType\":0,\n" +
                "\t\t\t\t\"completedLectureAmount\":0,\n" +
                "\t\t\t\t\"courseTemplateId\":1773920301481664512,\n" +
                "\t\t\t\t\"endAmount\":15,\n" +
                "\t\t\t\t\"regDivSegment\":1,\n" +
                "\t\t\t\t\"fullReg\":true,\n" +
                "\t\t\t\t\"gradeId\":31,\n" +
                "\t\t\t\t\"gradeName\":\"高一\",\n" +
                "\t\t\t\t\"isRecommend\":0,\n" +
                "\t\t\t\t\"lectureAmount\":15,\n" +
                "\t\t\t\t\"level\":4,\n" +
                "\t\t\t\t\"materialFee\":0.00,\n" +
                "\t\t\t\t\"periodId\":1718462212235673600,\n" +
                "\t\t\t\t\"periodName\":\"2019秋季\",\n" +
                "\t\t\t\t\"productId\":1760154237496762368,\n" +
                "\t\t\t\t\"progressStatus\":0,\n" +
                "\t\t\t\t\"segmentNo\":1,\n" +
                "\t\t\t\t\"seniorId\":1761221138115960832,\n" +
                "\t\t\t\t\"seniorName\":\"fb\",\n" +
                "\t\t\t\t\"startAmount\":1,\n" +
                "\t\t\t\t\"studyingFee\":1500.00,\n" +
                "\t\t\t\t\"studyingTime\":\"07:00-07:55\",\n" +
                "\t\t\t\t\"subjectId\":20,\n" +
                "\t\t\t\t\"subjectName\":\"数学\",\n" +
                "\t\t\t\t\"totalAmount\":1500.00\n" +
                "\t\t\t},\n" +
                "\t\t\t{\n" +
                "\t\t\t\t\"areaTeamId\":564701030634225664,\n" +
                "\t\t\t\t\"areaTeamName\":\"广州区域\",\n" +
                "\t\t\t\t\"busTeamId\":1747502052349169664,\n" +
                "\t\t\t\t\"busTeamName\":\"麻花业务组\",\n" +
                "\t\t\t\t\"campusId\":1761292520835567616,\n" +
                "\t\t\t\t\"campusName\":\"ys2校区\",\n" +
                "\t\t\t\t\"classDateTip\":\"每天\",\n" +
                "\t\t\t\t\"classId\":1774356736720613376,\n" +
                "\t\t\t\t\"className\":\"高二麻花暑5643\",\n" +
                "\t\t\t\t\"classNo\":\"260014\",\n" +
                "\t\t\t\t\"classType\":0,\n" +
                "\t\t\t\t\"completedLectureAmount\":0,\n" +
                "\t\t\t\t\"courseTemplateId\":1773943562756104192,\n" +
                "\t\t\t\t\"endAmount\":15,\n" +
                "\t\t\t\t\"regDivSegment\":1,\n" +
                "\t\t\t\t\"fullReg\":true,\n" +
                "\t\t\t\t\"gradeId\":32,\n" +
                "\t\t\t\t\"gradeName\":\"高二\",\n" +
                "\t\t\t\t\"isRecommend\":0,\n" +
                "\t\t\t\t\"lectureAmount\":15,\n" +
                "\t\t\t\t\"level\":1,\n" +
                "\t\t\t\t\"materialFee\":0.00,\n" +
                "\t\t\t\t\"periodId\":1718461895731625984,\n" +
                "\t\t\t\t\"periodName\":\"2019暑假\",\n" +
                "\t\t\t\t\"productId\":613027625962569728,\n" +
                "\t\t\t\t\"progressStatus\":0,\n" +
                "\t\t\t\t\"segmentNo\":1,\n" +
                "\t\t\t\t\"startAmount\":1,\n" +
                "\t\t\t\t\"studyingFee\":3000.00,\n" +
                "\t\t\t\t\"studyingTime\":\"08:00-10:00\",\n" +
                "\t\t\t\t\"subjectId\":20,\n" +
                "\t\t\t\t\"subjectName\":\"数学\",\n" +
                "\t\t\t\t\"totalAmount\":3000.00\n" +
                "\t\t\t},\n" +
                "\t\t\t{\n" +
                "\t\t\t\t\"areaTeamId\":564701030634225664,\n" +
                "\t\t\t\t\"areaTeamName\":\"广州区域\",\n" +
                "\t\t\t\t\"busTeamId\":1744931912258625536,\n" +
                "\t\t\t\t\"busTeamName\":\"苏业务组\",\n" +
                "\t\t\t\t\"campusId\":613012308330807296,\n" +
                "\t\t\t\t\"campusName\":\"誉优荔城富鹏教学中心\",\n" +
                "\t\t\t\t\"classDateTip\":\"每周三\",\n" +
                "\t\t\t\t\"classId\":1774754122760310784,\n" +
                "\t\t\t\t\"className\":\"暑荔城高二\",\n" +
                "\t\t\t\t\"classNo\":\"260338\",\n" +
                "\t\t\t\t\"classType\":0,\n" +
                "\t\t\t\t\"completedLectureAmount\":0,\n" +
                "\t\t\t\t\"courseTemplateId\":1774753830333435904,\n" +
                "\t\t\t\t\"endAmount\":15,\n" +
                "\t\t\t\t\"regDivSegment\":1,\n" +
                "\t\t\t\t\"fullReg\":true,\n" +
                "\t\t\t\t\"gradeId\":32,\n" +
                "\t\t\t\t\"gradeName\":\"高二\",\n" +
                "\t\t\t\t\"isRecommend\":0,\n" +
                "\t\t\t\t\"lectureAmount\":15,\n" +
                "\t\t\t\t\"level\":1,\n" +
                "\t\t\t\t\"materialFee\":0.00,\n" +
                "\t\t\t\t\"periodId\":1718461895731625984,\n" +
                "\t\t\t\t\"periodName\":\"2019暑假\",\n" +
                "\t\t\t\t\"productId\":1744988464709574656,\n" +
                "\t\t\t\t\"progressStatus\":0,\n" +
                "\t\t\t\t\"segmentNo\":1,\n" +
                "\t\t\t\t\"startAmount\":1,\n" +
                "\t\t\t\t\"studyingFee\":2000.00,\n" +
                "\t\t\t\t\"studyingTime\":\"08:00-10:00\",\n" +
                "\t\t\t\t\"subjectId\":20,\n" +
                "\t\t\t\t\"subjectName\":\"数学\",\n" +
                "\t\t\t\t\"totalAmount\":2000.00\n" +
                "\t\t\t}\n" +
                "\t\t],\n" +
                "\t\t\"student\":{\n" +
                "\t\t\t\"address\":\"\",\n" +
                "\t\t\t\"birthday\":1495814400000,\n" +
                "\t\t\t\"cid\":\"0B2FBA0F-F21F-11E8-B52F-005056B61F8C\",\n" +
                "\t\t\t\"createTime\":1495859802000,\n" +
                "\t\t\t\"gradeId\":99,\n" +
                "\t\t\t\"gradeName\":\"其他\",\n" +
                "\t\t\t\"lastRegCampusId\":1760158560570089472,\n" +
                "\t\t\t\"lastRegCampusName\":\"秀勤校区\",\n" +
                "\t\t\t\"lastRegTime\":1554978129000,\n" +
                "\t\t\t\"location\":\"\",\n" +
                "\t\t\t\"name\":\"温佐镜\",\n" +
                "\t\t\t\"phoneNo\":\"18312421977\",\n" +
                "\t\t\t\"referrerName\":\"\",\n" +
                "\t\t\t\"regClassAmount\":52,\n" +
                "\t\t\t\"remark\":\"\",\n" +
                "\t\t\t\"school\":\"广东 广州 越秀区 广州市第七中学\",\n" +
                "\t\t\t\"sex\":1,\n" +
                "\t\t\t\"studentId\":562481583026601984,\n" +
                "\t\t\t\"studentNo\":\"2017000001\",\n" +
                "\t\t\t\"tags\":\"员工子女\",\n" +
                "\t\t\t\"updateTime\":1543307582000\n" +
                "\t\t}\n" +
                "\t}\n" +
                "]", DiscountRegCourseClass.class )

        //[{"type":"string","name":"fromPeriodNames","value":"2018暑假,2018秋季","remark":"在读期数"},{"value":"2019寒假#1,2019春季","type":"string","name":"toPeriodNames","remark":"将要连报期期数"},{"type":"int","name":"subjectCount","value":"1","remark":"连报科数"},{"type":"float","name":"discount","value":"100","remark":"每科优惠金额"}]
        def p  = ["fromPeriodNames":"2019寒假,2019春季","toPeriodNames":"2019暑假#1,2019秋季" , "discount" : 100 ,"subjectCount":1 ] ;

        def s = new ExecuteServiceImpl()
        def execute = s.execute(array, p, new Helper(null, null, null) {

            @Override
            Long countStudentRegAmount(Long studentId, Long areaTeamId, String periodName) {
                return 1
            }

            @Override
            boolean isOldStudent(Long studentId, Long periodId) {
                return false
            }

            @Override
            boolean hasDiscount(Long studentId, Long classId, Long discountId) {
                return true
            }

            @Override
            boolean hasReg(Long studentId, Long classId, Integer startAmount, Integer endAmount) {
                return true
            }

            @Override
            boolean hasReg(Long studentId, Long classId, Integer startAmount, Integer endAmount, String regTime) {
                String[] times = regTime.split("~");
                return true
            }
        })

        println(JSON.toJSONString( execute ))


    }
}

